<template>
	<view class="content">
		<view class="header">
			<view class="search-view">
				<input class="input" type="text" placeholder-style="color:#999999" placeholder="请输入关键词"
					@confirm="searchAction" v-model="keywords" />
				<image class="search-icon" src="../../static/images/icon_search.png"></image>
			</view>
			<view class="tap">
				<view class="tap-item" :class="tap == 0 ? 'active':''" data-index="0" @click="changeTap">
					商品
				</view>
				<view class="tap-item" :class="tap == 1 ? 'active':''" data-index="1" @click="changeTap">
					资讯
				</view>
				<!-- <view class="tap-item" :class="tap == 2 ? 'active':''" data-index="2" @click="changeTap">
					藏友晒宝
				</view> -->
			</view>
		</view>
		<view class="list" v-if="tap==0">
			<view class="goods-list">
				<goodsItem class="goodsItem" v-for="(item,index) in datasource" :dataObject="item" :key="index"></goodsItem>
			</view>
		</view>
		<view class="list" v-if="tap==1">
			<informationItem v-for="(item,index) in datasource" :key="index" :data="item"></informationItem>
		</view>
		<view class="list" v-if="tap==2" style="margin-top: 202upx;">
			<bbsItem v-for="(item,index) in datasource" :key="index" :dataSource="item"></bbsItem>
		</view>
		<noMoreData v-if="isBottom && datasource.length>0"></noMoreData>
	</view>
</template>

<script>
	import goodsItem from '../index/components/goodsItem.vue'
	import informationItem from '../index/components/information-item.vue'
	import bbsItem from '../index/components/bbs-item.vue'
	import noMoreData from '@/components/noMoreData/index.vue'
	import noData from '@/components/noData/index.vue'
	import pageFooter from '@/components/pageFooter/index.vue'
	import {
		trendTreasure,
		articleList
	} from '@/api/peopleHide.js'
	import {
		getSearchKeyword,
		getProductslist,
	} from '@/api/store.js';
	export default {
		components: {
			goodsItem,
			informationItem,
			bbsItem,
			noMoreData,
			noData
		},
		data() {
			return {
				tap: 0,
				page: 1,
				limit: 15,
				keywords: '',
				isBottom: false,
				datasource: []
			}
		},
		onLoad() {
			this.getDatasource()
		},
		onReachBottom() {
			if(!isBottom){
				this.page++
				this.getDatasource()
			}
		},
		methods: {
			searchAction() {
				this.page = 1
				this.getDatasource()
			},
			getDatasource() {
				if (this.tap == 0) {
                    this.getProductslist()
				} else if (this.tap == 1) {
					this.articleList()
				} else {
					this.trendTreasure()
				}
			},
			articleList() { //资讯
				articleList({
					page: this.page,
					limit: this.limit,
					keywords: this.keywords
				}).then(res => {
					let data = []
					if (this.page == 1) {
						data = res.data
					} else {
						data = this.datasource.concat(res.data)
					}
					this.datasource = data
					this.isBottom = res.data.length < this.limit ? true : false
				})
			},
			getProductslist(){//商品搜索
				getProductslist({
					keyword: this.keywords,
					page: this.page,
					limit: this.limit
				}).then(res=>{
					let data = []
					if (this.page == 1) {
						data = res.data
					} else {
						data = this.datasource.concat(res.data)
					}
					this.datasource = data
					this.isBottom = res.data.length < this.limit ? true : false
				})
			},
			trendTreasure() { //晒宝
				trendTreasure({
					page: this.page,
					limit: this.limit,
					keywords: this.keywords
				}).then(res => {
					let data = []
					if (this.page == 1) {
						data = res.data
					} else {
						data = this.datasource.concat(res.data)
					}
					this.datasource = data
					this.isBottom = res.data.length < this.limit ? true : false
				})
			},
			changeTap(e) {
				let index = e.currentTarget.dataset.index
				if (this.tap != index) {
					this.tap = index
					this.page = 1
					this.keywords = ''
					this.getDatasource()
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f4f5f6;
	}

	.content {
		padding-bottom: 30upx;
	}

	.header {
		display: flex;
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
		flex-direction: column;
		align-items: center;
		z-index: 99;
		background-color: #fff;
		box-shadow: 0px 4rpx 8rpx 0px rgba(236, 236, 236, 0.50);
		box-sizing: border-box;

		.search-view {
			width: 690upx;
			height: 64upx;
			margin-top: 24upx;
			background-color: #F9F9F9;
			border-radius: 32upx;
			display: flex;
			align-items: center;
			justify-content: center;
			position: relative;

			.input {
				font-size: 30upx;
				text-align: center;
			}

			.search-icon {
				width: 28rpx;
				height: 28rpx;
				position: absolute;
				right: 20upx;
			}
		}

		.tap {
			width: 100%;
			height: 94upx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.tap-item {
				display: flex;
				align-items: center;
				justify-content: center;
				height: 100%;
				flex: 1;
				position: relative;
				font-size: 32upx;
				font-weight: 400;
			}

			.active {
				color: #0A77DE;
				font-weight: 500;
			}

			.active:after {
				position: absolute;
				content: '';
				bottom: 0;
				width: 50upx;
				background-color: #0A77DE;
				height: 2upx;
			}
		}
	}

	.list {
		display: flex;
		flex-direction: column;
		width: 100%;
		margin-top: 182upx;
		align-items: center;
	}

	.magtop {
		margin-top: 202upx;
	}

	.goods-list {
		width: 100%;
		display: flex;
		flex-wrap: wrap;
		padding: 0 30upx;
		box-sizing: border-box;
	}

	.goodsItem {
		margin-right: 30rpx;
	}

	.goodsItem:nth-of-type(2n) {
		margin-right: 0;
	}
</style>
